﻿
#region Usings

using PolyGameEngine.GamePieces;
using PolyGameEngine.GamePieces.Common;
using PolyGameEngine.Tools;

using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Factories;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

#endregion
namespace PolyGameEngine.LevelTools
{
    public class Border : DrawableGameComponent
    {
        #region Fields
        PhysicsGame game;
        World world;
        Body border;
        Texture2D TopSprite;
        Texture2D BottomSprite;
        Texture2D LeftSprite;
        Texture2D RightSprite;
        Vector2 size;
        SpriteBatch sb;
        public Fixture ColidingFixture;
        #endregion

        Vector2 screenOffset
        {
            get { return GamePiece._ScreenOffset; }
        }

        public Border(PhysicsGame Game, World World, Vector2 Size, Texture2D Texture)
            : base(Game)
        {
            game = Game;
            world = World;
            sb = Game.spriteBatch;
            size = Size;

            border = BodyFactory.CreateBody(World, ConvertUnits.ToSimUnits(Size / 2));
            border.CollisionCategories = Category.All;
            border.CollidesWith = Category.All;

            //Top
            FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(Size.X), //width
                ConvertUnits.ToSimUnits(10),                                //Height
                1,                                                          //Density
                ConvertUnits.ToSimUnits(new Vector2(0, -(Size.Y / 2 - 5))),//offset
                border);
            TopSprite = Game.assetcreator.TextureFromShape(border.FixtureList[0].Shape, new Material(Texture, "Border"), Color.White, 2);

            //Bottom
            FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(Size.X), //width
                ConvertUnits.ToSimUnits(10),                                //Height
                1,                                                          //Density
                ConvertUnits.ToSimUnits(new Vector2(0, (Size.Y / 2 - 5))),//offset
                border);
            BottomSprite = Game.assetcreator.TextureFromShape(border.FixtureList[1].Shape, new Material(Texture, "Border"), Color.White, 2);

            //Left
            FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(10), //width
                ConvertUnits.ToSimUnits(Size.Y - 10),                                //Height
                1,                                                          //Density
                ConvertUnits.ToSimUnits(new Vector2(-(Size.X / 2 - 5), 0)),//offset
                border);
            LeftSprite = Game.assetcreator.TextureFromShape(border.FixtureList[2].Shape, new Material(Texture, "Border"), Color.White, 2);

            //Right
            FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(10), //width
                ConvertUnits.ToSimUnits(Size.Y - 10),                                //Height
                1,                                                          //Density
                ConvertUnits.ToSimUnits(new Vector2((Size.X / 2 - 5), 0)),//offset
                border);
            RightSprite = Game.assetcreator.TextureFromShape(border.FixtureList[3].Shape, new Material(Texture, "Border"), Color.White, 2);

            //collision eventHandlers
            border.FixtureList[0].OnCollision += HandleBorderCollision;
            border.FixtureList[1].OnCollision += HandleBorderCollision;
            border.FixtureList[2].OnCollision += HandleBorderCollision;
            border.FixtureList[3].OnCollision += HandleBorderCollision;
            border.FixtureList[0].OnSeparation += HandleAfterBorderCollision;
            border.FixtureList[1].OnSeparation += HandleAfterBorderCollision;
            border.FixtureList[2].OnSeparation += HandleAfterBorderCollision;
            border.FixtureList[3].OnSeparation += HandleAfterBorderCollision;




        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                base.Dispose(disposing);
        }

        bool colidedWithBorder = false;
        public bool HandleBorderCollision(Fixture border, Fixture colider, Contact contact)
        {
            if (colider.UserData != null &&
                colider.UserData.GetType().IsSubclassOf(typeof(Unit)))
            {
                ColidingFixture = colider;
                colidedWithBorder = true;
            }
            return true;

        }
        public void HandleAfterBorderCollision(Fixture border, Fixture colider)
        {
            if (colider.UserData != null && colider.UserData.GetType().IsSubclassOf(typeof(Unit))
                 && colidedWithBorder)
            {
                colidedWithBorder = false;
                ((Unit)(colider.UserData)).canJump = false;
                ColidingFixture = null;
            }
        }


        public override void Draw(GameTime gameTime)
        {
                sb.Draw(TopSprite, new Vector2(0, 0) + screenOffset, Color.White);
                sb.Draw(BottomSprite, new Vector2(0, size.Y - 10) + screenOffset, Color.White);
                sb.Draw(LeftSprite, new Vector2(0, 10) + screenOffset, Color.White);
                sb.Draw(RightSprite, new Vector2(size.X - 10, 10) + screenOffset, Color.White);
           
            base.Draw(gameTime);
        }
    }
}
